package com.example.democlient;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Application;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.example.democlient.model.Task;
import com.example.democlient.network.SocketService;

public class MyApplication extends Application {
	
	static MyApplication mDemoApp;
	int totalDistanceM = 0;
	public String account="";
	public List<Task> taskList;
	public List<Task> roadDesignList;
    public static int flag = 0;
	private static final Map<Class<? extends Activity>,Handler> activityContainer = new HashMap<Class<? extends Activity>,Handler>();
    
    public void setHandler(Class<? extends Activity> classz,Handler handler){
    	activityContainer.put(classz,handler);
    }
    
    public Handler getHander(Class<? extends Activity> classz)
    {
    	return activityContainer.get(classz);
    }
    
    
	//百度MapAPI的管理类
	BMapManager mBMapMan = null;
	
	// 授权Key
	String mStrKey = "EE83FA328B05471030007D3B63F1A20A500DF050";
	boolean m_bKeyRight = true;	// 授权Key正确，验证通过
	
	//常用事件监听，用来处理通常的网络错误，授权验证错误等
	static class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {
			Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError);
			Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "您的网络出错啦！",Toast.LENGTH_LONG).show();
		}

		@Override
		public void onGetPermissionState(int iError) {
			Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError);
			if (iError ==  MKEvent.ERROR_PERMISSION_DENIED) {
				//授权Key错误：
				Toast.makeText(MyApplication.mDemoApp.getApplicationContext(), "请在BMapApiDemoApp.java文件输入正确的授权Key！",Toast.LENGTH_LONG).show();
				MyApplication.mDemoApp.m_bKeyRight = false;
			}
		}
	}

	@Override
    public void onCreate() {
		mDemoApp = this;
		mBMapMan = new BMapManager(this);
		boolean isSuccess = mBMapMan.init(this.mStrKey, new MyGeneralListener());
		// 初始化地图sdk成功，设置定位监听时间
		if (isSuccess) {
		   // mBMapMan.getLocationManager().setNotifyInternal(10, 5);
		}
		else {
			
		}
		SocketService.getInstance();
		super.onCreate();
	}

	@Override
	//建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
	public void onTerminate() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
	}
	
	private List<Activity> activityList=new LinkedList<Activity>();
	
	//添加Activity 到容器中
	public void addActivity(Activity activity)
	{
	  activityList.add(activity);
	}
	
	//遍历所有Activity 并finish
	public void exit()
	{
		for(Activity activity:activityList)
		{
		  activity.finish();
		}
	    System.exit(0);
	}

}
